package com.cmcc.es.app.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import com.cmcc.es.app.mapper.MessageSyncMapper;
import com.cmcc.es.app.model.Message;
import com.cmcc.es.app.model.MessageSyncData;
import com.cmcc.es.app.service.DataSyncService;


import cc.movein.common.utils.XmlTools;

/**
 * 我的消息
 * @author WZX
 *
 */
@Service("messageSyncService")
public class MessageSyncServiceImpl implements DataSyncService{
	private Log log = LogFactory.getLog(MessageSyncServiceImpl.class);
	private static final String DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";
	@Autowired
	private MessageSyncMapper messageSyncMapper;
	@Transactional
	@Override
	public void doSync(String xml) {
		String decodedXml = null;
		try {
			decodedXml = new String(Base64Utils.decodeFromString(xml), "utf-8");
			log.debug(decodedXml);
			MessageSyncData data = XmlTools.xmlStringToBean(decodedXml, MessageSyncData.class, DATA_FORMAT);
			log.info(data);
			
			List<Message> insertList = data.getInsertList();
			if(insertList != null && insertList.size()>0){
				messageSyncMapper.insertList(insertList);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
